package com.fy.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.fy.entity.EpcUser;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;

public class JwtUtils {

    public static String getToken(EpcUser user) {
        String token="";
//        Date date = DateUtils.addHours(new Date(), 1);
//        token= JWT.create().withAudience(String.valueOf(user.getId()))
//                //.withExpiresAt(date)
//                .withClaim("user", JSON.toJSONString(user))
//                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }

    public static String getToken(EpcUser user,String sessionId) {
        String token="";
//        Date date = DateUtils.addHours(new Date(), 1);
//        token= JWT.create().withAudience(String.valueOf(user.getId()))
//                //.withExpiresAt(date)
//                .withClaim("user", JSON.toJSONString(user))
//                .withClaim("sessionId",JSONObject.toJSONString(sessionId))
//                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }

    public static EpcUser getUserInfo(String token){
        String userString = JWT.decode(token).getClaim("user").asString();
        EpcUser userInfo = JSONObject.parseObject(userString, EpcUser.class);
        return userInfo;
    }

    public static String getSessionId(String token){
        return JWT.decode(token).getClaim("sessionId").asString();
    }

    public static void main(String[] args) {

//        UserInfo userInfo = new UserInfo();
//        userInfo.setId(1);
//        userInfo.setPwd("123");
//        String token1 = getToken(userInfo);
//        System.out.println(token1);
//        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6MTU3NjcyOTQzOH0.fWH-imSEYhXbPCr9UJnANC1iK4JYdrISa6J4CgWXo4k";
////        JWT.decode(token).getAudience().get(0);
////        DecodedJWT decode = JWT.decode(token);
////        decode.getAudience();
////        decode.getExpiresAt();
////        System.out.println(JWT.decode(token).getAudience().get(0));
//        JWT.decode(token1).getAudience().forEach(f-> System.out.println(f));
//        String user = JWT.decode(token1).getClaim("user").asString();
//        System.out.println(user);
    }

}
